package com.alibaba.csp.sentinel.dashboard.rule.apollo;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.fastjson.JSON;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;


/**
 * @ClassName ParamFlowRuleNacosPublisher
 * @Description TODO
 * @Author zhouchao
 * @Date 2020/2/17 20:25
 * @Version 1.0
 */
@Component("paramFlowRuleApolloPublisher")
public class ParamFlowRuleApollPublisher extends AbstractApolloCommonService implements
        DynamicRulePublisher<List<ParamFlowRuleEntity>> {

    @Override
    public void publish(String appName, List<ParamFlowRuleEntity> rules) throws Exception {
        List<ParamFlowRule> paramFlowRules = rules.stream().map(r -> {
            ParamFlowRule paramFlowRule = new ParamFlowRule();
            BeanUtils.copyProperties(r, paramFlowRule);
            return paramFlowRule;
        }).collect(Collectors.toList());
        apolloCommonService.publishRules(appName, paramFlowDataIdSuffix, JSON.toJSONString(paramFlowRules));
    }
}
